package com.softtown.modules.organization.controller;

import com.github.pagehelper.PageInfo;
import com.softtown.common.annotation.LogOperation;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.regularUtils.RegularUtils;
import com.softtown.common.utils.Result;
import com.softtown.common.validator.ValidatorUtils;
import com.softtown.common.validator.group.DefaultGroup;
import com.softtown.modules.organization.dto.CompOrgDeptDTO;
import com.softtown.modules.organization.dto.CompOrgHstDeptDTO;
import com.softtown.modules.organization.entity.CompOrgDeptEntity;
import com.softtown.modules.organization.entity.CompOrgHstDeptEntity;
import com.softtown.modules.organization.service.CompOrgDeptService;
import com.softtown.modules.organization.service.CompOrgHstDeptService;
import com.softtown.modules.organization.vo.CompOrgDeptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @version v1.0
 * @ProjectName: softtown-aioffice
 * @ClassName: CompOrgHstDeptController
 * @Description: 公司部门变更履历Controller层
 * @Author: fuming
 * @Date: 2019/6/21 22:48
 */
@RestController
@RequestMapping("/company/hst")
@Api(tags = "公司部门变更履历")
public class CompOrgHstDeptController {

    @Resource
    private CompOrgHstDeptService compOrgHstDeptService;

    @Resource
    private CompOrgDeptService compOrgDeptService;

    /**
     * @param @param [compOrgDeptDTO]
     * @return @return com.softtown.common.utils.Result
     * @throws
     * @Title: addDeptHstInfo
     * @Description: 增加部门履历信息
     * @author fuming
     * @date 2019/6/22 0:40
     */
    @PostMapping("addHst")
    @ApiOperation("添加部门履历信息")
    @LogOperation("添加部门履历信息")
    public Result addDeptHstInfo(@RequestBody CompOrgDeptDTO compOrgDeptDTO) {
        Result result = new Result<>();
        ValidatorUtils.validateEntity(compOrgDeptDTO, DefaultGroup.class);
        if (!RegularUtils.isNum(String.valueOf(compOrgDeptDTO.getSort()))) {
            return result.error(ErrorCode.INCORRECT_SORT_FORMAT);
        }
        List<CompOrgDeptVO> orgListByCompId = compOrgDeptService.getOrgListByCompId(compOrgDeptDTO.getCompId());
        for (CompOrgDeptVO orgDept:orgListByCompId) {
            String deptNames = orgDept.getName();
            Long id = orgDept.getId();
            if (compOrgDeptDTO.getName().equals(deptNames) && !compOrgDeptDTO.getId().equals(id)) {
                return result.error(ErrorCode.DEPARTMENT_NAME_EXISTS);
            }
        }
        CompOrgDeptEntity orgDeptEntity = compOrgDeptService.getById(compOrgDeptDTO.getPidNew());
        if (orgDeptEntity != null && compOrgDeptDTO.getNameOld().equals(orgDeptEntity.getName())) {
            return result.error(ErrorCode.SUPERIOR_DEPT_ERROR);
        }
        return compOrgDeptService.updateDeptInfos(compOrgDeptDTO);
    }

    /**
     * @param @param [compOrgHstDeptDTO]
     * @return @return com.softtown.common.utils.Result
     * @throws
     * @Title: getDeptHstList
     * @Description: 获取部门变更履历分页
     * @author fuming
     * @date 2019/7/4 20:10
     */
    @PostMapping("getDeptHstList")
    @ApiOperation("获取部门履历信息分页")
    @LogOperation("获取部门履历信息分页")
    public Result getDeptHstList(@RequestBody CompOrgHstDeptDTO compOrgHstDeptDTO) {
        PageInfo<CompOrgHstDeptEntity> deptHstListByCompId = compOrgHstDeptService.getDeptHstListByCompId(compOrgHstDeptDTO);
        return new Result<PageInfo<CompOrgHstDeptEntity>>().ok(deptHstListByCompId);
    }
}
